package cn.com.yx.ms.controller;

import cn.com.yx.ms.application.web.ApiPager;
import cn.com.yx.ms.application.web.ApiQuery;
import cn.com.yx.ms.application.web.ApiQueryBase;
import cn.com.yx.ms.domain.dbmodel.ScLive;
import cn.com.yx.ms.service.IProgramService;
import cn.yx.framework.configuration.ApiCode;
import cn.yx.framework.configuration.AppConstant;
import cn.yx.framework.util.MsgPropertyUtil;
import cn.yx.framework.view.ApiResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * 应用中心
 */
@RestController
@RequestMapping(value = "/")
@Api(value = "直播流管理", description = "获取或操作直播流等资源")
public class LivesController {
    private static final Logger logger = LoggerFactory.getLogger(LivesController.class);
    @Resource
    private MessageSource messageSource;
    @Resource
    private IProgramService iProgramService;
    @Resource
    private RedisTemplate redisTemplate;


    @PostMapping("live")
    @ApiOperation(value = "创建直播流", notes = "创建直播流")
    @ApiImplicitParam(paramType = "body", name = "scFileModel", dataType = "Map", required = true, value = "直播流详细信息")
    public ApiResult createLive(@RequestBody @Validated ScLive scLive, BindingResult bindingResult) throws Exception {
        ApiCode apiCode = iProgramService.saveLive(scLive);
        return new ApiResult<>(AppConstant.MSG_SUCCESS,
                apiCode == ApiCode.APP_100200,
                MsgPropertyUtil.getProperty(apiCode.msgId),
                null);
    }

    @DeleteMapping("lives/{id}")
    @ApiOperation(value = "删除直播流信息", notes = "删除直播流信息")
    @ApiImplicitParam(paramType = "path", name = "id", dataType = "String", required = true, value = "角色id")
    public ApiResult deleteLive(@PathVariable("id") String id) {
        ApiCode apiCode = iProgramService.deleteLive(id);
        return new ApiResult<>(AppConstant.MSG_SUCCESS,
                apiCode == ApiCode.APP_100200,
                MsgPropertyUtil.getProperty(apiCode.msgId),
                null);

    }

    @PutMapping("live")
    @ApiOperation(value = "更新直播流信息", notes = "更新直播流信息")
    @ApiImplicitParam(paramType = "body", name = "scFileModel", dataType = "Map", required = true, value = "直播流实体详细信息")
    public ApiResult updateLiveModel(@RequestBody @Validated ScLive scLive, BindingResult bindingResult) {
        ApiCode apiCode = iProgramService.saveLive(scLive);
        return new ApiResult<>(AppConstant.MSG_SUCCESS,
                apiCode == ApiCode.APP_100200,
                MsgPropertyUtil.getProperty(apiCode.msgId),
                null);
    }

    @ResponseBody
    @RequestMapping(value = "lives/{id}", method = GET)
    @ApiImplicitParam(paramType = "String", name = "id", dataType = "String", required = true, value = "角色ID")
    @ApiOperation("获取单个直播流素材信息")
    public ApiResult findLiveById(@PathVariable("id") String id) {
        return new ApiResult<>(AppConstant.MSG_SUCCESS, iProgramService.findOneLive(id));
    }

    @ResponseBody
    @RequestMapping(value = "lives/page", method = RequestMethod.GET)
    @ApiOperation("获取带分页的直播流信息")
    public ApiResult findPage(@ApiQuery ApiQueryBase query, HttpServletRequest request) {

        // 查询条件接受
        query.getConditions().put("title", request.getParameter("title"));
        query.getConditions().put("channelno",request.getParameter("channelno"));
        query.getConditions().put("ps", request.getParameter("ps"));
        query.getConditions().put("pi", request.getParameter("pi"));



        // 翻页对象构建
        PageHelper.startPage(query.getPi(), query.getPs(), true);
        Page<ScLive> page = (Page<ScLive>) iProgramService.findLives(query);
        ApiPager<ScLive> apiPager = new ApiPager<ScLive>(query.getPs(),
                query.getPi(),
                page.getTotal(),
                page.getResult());

        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", apiPager);

    }

    @ResponseBody
    @RequestMapping(value = "lives", method = RequestMethod.GET)
    @ApiOperation("获取所有直播流信息")
    public ApiResult find() {
        return new ApiResult<>(AppConstant.MSG_SUCCESS,
                true,
                "",
                iProgramService.findLives(null));
    }


}